﻿using Basic.HrGatewayModular.Interface;
using Basic.HrGatewayModular.Model.EmpSign;
using Basic.HrRemoteModel.EmpSignatureFile;
using Basic.HrRemoteModel.EmpSignatureFile.Model;
using Basic.HrRemoteModel.PdfSign;
using Basic.HrRemoteModel.PdfSign.Model;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.HttpApiGateway.Model;
using WeDonekRpc.Modular;
using WeDonekRpc.Modular.Model;
using WeDonekRpc.ModularModel.Accredit.Model;

namespace Basic.HrGatewayModular.Services
{
    internal class EmpSignFileService : IEmpSignFileService
    {
        private readonly IAccreditService _Accredit;

        public EmpSignFileService ( IAccreditService accredit )
        {
            this._Accredit = accredit;
        }

        public string AddAccredit ( long empId, ApplyAccredit obj )
        {
            if ( obj.AccreditId.IsNull() )
            {
                return this._AddAccredit(empId, obj);
            }
            else
            {
                return this._SetAccredit(empId, obj);
            }
        }
        private string _SetAccredit ( long empId, ApplyAccredit obj )
        {
            IUserState state = this._Accredit.GetAccredit(obj.AccreditId);
            if ( state.GetValue<long>("EmpId") != empId )
            {
                throw new ErrorException("hr.emp.no.power");
            }
            List<long> fileId = state.GetValue<List<long>>("FileId");
            if ( fileId.Contains(obj.Id) )
            {
                return obj.AccreditId;
            }
            this._CheckPwd(empId, obj);
            fileId.Add(obj.Id);
            if ( !state.SaveState() )
            {
                throw new ErrorException("hr.emp.accredit.save.fail");
            }
            return obj.AccreditId;
        }
        private string _AddAccredit ( long empId, ApplyAccredit obj )
        {
            this._CheckPwd(empId, obj);
            UserState state = new UserState();
            state.Param.Add("EmpId", new StateParam(empId));
            state.Param.Add("FileId", new StateParam(new List<long>()
            {
                obj.Id
            }));
            return this._Accredit.ApplyTempAccredit(false, state, 3600);
        }
        private void _CheckPwd ( long empId, ApplyAccredit obj )
        {
            new CheckSignFilePower
            {
                EmpId = empId,
                Pwd = obj.Pwd,
                SignFileId = obj.Id
            }.Send();
        }
        public long Add ( EmpSignAdd datum, long empId )
        {
            return new AddSignFile
            {
                Datum = new SelfSignFileAdd
                {
                    Email = datum.Email,
                    Dept = datum.Dept,
                    BeginTime = datum.BeginTime,
                    CertPwd = datum.CertPwd,
                    EmpId = empId,
                    EncryptionType = datum.EncryptionType,
                    EndTime = datum.EndTime,
                    Company = datum.Company,
                    Country = datum.Country,
                    SignName = datum.SignName
                }
            }.Send();
        }
        public long UpSign ( UpEmpSign datum, long empId )
        {
            return new AddUpSignFile
            {
                Datum = new UpSignFileAdd
                {
                    CertPwd = datum.CertPwd,
                    EmpId = empId,
                    FileId = datum.FileId
                }
            }.Send();
        }
        public PagingResult<SignFileDto> Query ( long empId, PagingParam<SignQueryParam> query )
        {
            return new QuerySignFile
            {
                EmpId = empId,
                Index = query.Index,
                Query = query.Query,
                IsDesc = query.IsDesc,
                NextId = query.NextId,
                Size = query.Size,
                SortName = query.SortName
            }.Send();
        }
        public void Delete ( long id, long empId )
        {
            new DropSignFile
            {
                Id = id,
                EmpId = empId
            }.Send();
        }
        public SignFileItem[] Gets ( long empId )
        {
            return new GetSignFileList
            {
                EmpId = empId
            }.Send();
        }

        public SignResult PdfSign ( long empId, string accreditId, WriteSignPdf signPdf )
        {
            IUserState state = this._Accredit.GetAccredit(accreditId);
            if ( state.GetValue<long>("EmpId") != empId )
            {
                throw new ErrorException("hr.emp.no.power");
            }
            long[] signId = state.GetValue<long[]>("FileId");
            long[] ids = signPdf.Sign.ConvertAll(c => c.SignFileId);
            if ( !ids.IsExists(signId) )
            {
                throw new ErrorException("hr.sign.file.no.accredit");
            }
            return new GenerateSignPdf
            {
                EmpId = empId,
                SignPdf = signPdf
            }.Send();
        }
    }
}
